﻿var me = this;
var vm = this.lookupViewModel();

if (!requestData.bizModel.companyResourcesRequest.newBundFloors) {
    requestData.bizModel.companyResourcesRequest.newBundFloors = {
        f7: 0,
        f8: 0,
        f9: 0,
        f10: 0,
        f11: 0,
        f12: 0,
        facialRecognition: 0,
        fireExits: 0,
        serverRoom: 0
    };
}

if (me.analysis.status == 'launch' || me.analysis.status == 'resubmit') {

    var vm = this.lookupViewModel();

    var fieldList = ['nameEnglish', 'nameFirst', 'surname'],
        showTipsMsg = function (msg) {
            Ext.toast({
                html: msg,
                closable: false,
                align: 't',
                slideDUration: 400,
                maxWidth: 400
            });
        };

    var lang = window.localStorage.getItem('PAI-language');
    var errorMsg = {
        zh_CN: {
            pinyin: '{0}: 请输入英文或拼音.',
            formatIncorrect: '{0}: 请输入正确的证件号码.',
            cardUniqueness: '{0}: 已存在 请更改.'
        },
        en: {
            pinyin: '{0}: Please input English or Spelling.',
            formatIncorrect: '{0}: Format Error.',
            cardUniqueness: '{0}: Already Exists'
        }
    };
    var hanE = /^[a-zA-Z0-9_%&',;\s=?$@\x22]+$/;

    for (var i = 0; i < fieldList.length; i++) {
        var field = fieldList[i];
        var fieldValue = ref[field].getValue();

        if (!hanE.test(fieldValue)) {
            showTipsMsg(Ext.String.format(errorMsg[lang].pinyin, ref[field].fieldLabel));
            return false;
        }
    }
    if (!(/^1[3456789]\d{9}$/.test(ref.mobilePhone.getValue()))) {
        showTipsMsg(lang == 'zh_CN' ? '手机号码格式不符合' : 'Mobile Phone：Format Error');
        return false;
    }

    var idType = ref.idType.getValue();

    var idcardReg = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
    var passportReg = /^[a-zA-Z0-9]{8,21}$/;
    var cardNo = ref.idNoOrPassportNo.getValue();
    switch (idType) {
        case 'idCard':
            if (!idcardReg.test(cardNo)) {
                showTipsMsg(Ext.String.format(errorMsg[lang].formatIncorrect, 'ID No.'));
                return false;
            }
            break;
        case 'passport':
            if (!passportReg.test(cardNo)) {
                showTipsMsg(Ext.String.format(errorMsg[lang].formatIncorrect, 'Passport No.'));
                return false;
            }
            break;
    }

    var companyResourcesValue = ref.companyResources.getValue();

    if (companyResourcesValue.accessCard == 1 && Ext.isEmpty(vm.get('NebulogyForm.file1'))) {
        showTipsMsg('Attachment is mandatory');
        return false;
    }


    //通过了校验就验证唯一性.
    var cardValid = true;
    Ext.PAIRest.request({
        ns: PAI.application.url,
        url: 'last/hrm_agencyBoarding/page',
        method: "POST",
        async: false,
        data: {
            idNoOrPassportNo: cardNo,
            pushStatus: 1
        },
        success: function (result) {
            if (result.hrm_agencyBoardingList.length > 0) {

                cardValid = result.hrm_agencyBoardingList.filter(function (record) {
                    return record.idNoOrPassportNo == cardNo;
                }).length == 0;

            }
        }
    });

    if (!cardValid) {
        showTipsMsg(Ext.String.format(errorMsg[lang].cardUniqueness, idType == 'idCard' ? 'ID No.' : 'Passport.'));
        return cardValid;
    }
} else if (me.analysis.status == 'approval') {
    //CDSID 唯一校验
    var cdsidValid = true;
    if (ref.approverCDSID.getValue()) {

        Ext.PAIRest.request({
            ns: PAI.application.url,
            url: 'last/hrm_agencyBoarding/page',
            method: "POST",
            async: false,
            data: {
                approverCDSID: ref.approverCDSID.getValue(),
                pushStatus: 1
            },
            success: function (result) {
                if (result.hrm_agencyBoardingList.length > 0) {

                    cdsidValid = result.hrm_agencyBoardingList.filter(function (record) {
                        return record.approverCDSID == ref.approverCDSID.getValue();
                    }).length == 0;

                }
            }
        });
    }

    if (!cdsidValid) {
        Ext.Msg.alert('System Prompt', 'CDSID: Already Exists');
        return cdsidValid;
    }
}




